package com.qf.agro.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.common.sys.SysUserHolder;
import com.qf.agro.mapper.sys.SysModuleMapper;
import com.qf.agro.pojo.sys.SysModule;
import com.qf.agro.service.sys.SysModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysModelServiceImpl implements SysModuleService {

    @Autowired
    private SysModuleMapper sysModuleMapper;

    @Override
    public Page<SysModule> findPage(Integer pageNum, Integer pageSize, String name) {
        //1.创建分页page对象
        Page<SysModule> page = new Page<>(pageNum, pageSize);
        //2.创建查询条件对象
        LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
        //3.设置查询条件
        queryWrapper.like(name != null, SysModule::getName, name);
        //4.分页查询
        page = sysModuleMapper.selectPage(page, queryWrapper);
        //5.返回
        return page;
    }

    @Override
    public void save(SysModule sysModule) {

        sysModuleMapper.insert(sysModule);
    }


    @Override
    public void disabled(Long id) {

        SysModule sysModule = new SysModule();
        LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysModule::getId, id);
        sysModuleMapper.update(sysModule, queryWrapper);//update user  set state = 0 where user_id = id;
    }

    @Override
    public void update(SysModule sysModule) {

        sysModuleMapper.updateById(sysModule);
    }

    @Override
    public List<SysModule> list() {
        LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(SysModule::getParentName);
        List<SysModule> sysModules = sysModuleMapper.selectList(queryWrapper);
        for (SysModule sysModule : sysModules) {
            LambdaQueryWrapper<SysModule> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysModule::getParentId, sysModule.getId());
            List<SysModule> children = sysModuleMapper.selectList(wrapper);
            sysModule.setChildren(children);
        }
        return sysModules;
    }

    @Override
    public List<Long> findByRoleId(Long roleId) {
        return sysModuleMapper.selectByRoleId(roleId);
    }


    @Override
    public void updateUserModule(Long roleId, List moduleIds) {
        sysModuleMapper.deleteByRoleId(roleId);
        if (moduleIds != null && moduleIds.size() > 0) {
            //2.为当前角色分配新的权限
            sysModuleMapper.insertRoleModule(roleId, moduleIds);
        }
    }


    @Override
    public List<SysModule> findUserMenu() {
        //1.获取当前登录的用户id
        Long userId = SysUserHolder.get().getId();
        //2.根据用户id，查询用户可以操作的一级菜单
        List<SysModule> parent = sysModuleMapper.findByUserId(userId);
        //3.遍历一级菜单
        for (SysModule sysModule : parent) {
            //4.创建查询条件
            LambdaQueryWrapper<SysModule> queryWrapper = new LambdaQueryWrapper<>();
            //5.设置条件
            queryWrapper.eq(SysModule::getParentId,sysModule.getId());
            //6.查询
            List<SysModule> children = sysModuleMapper.selectList(queryWrapper);
            //7.给父菜单设置子菜单
            sysModule.setChildren(children);
        }
        //8.返回
        return parent;
    }
}
